opendir $dir, $ARGV[0] or die "can not open the directory $ARGV[0]!\n";
open Output, ">$ARGV[0]\\oneSVM.info" or die "$ARGV[0]\\oneSVM.info";
open Output1, ">$ARGV[0]\\mulSVM.info" or die "$ARGV[0]\\mulSVM.info";
open Train, ">$ARGV[0]\\oneSVM_train.info" or die "$ARGV[0]\\oneSVM_train.info";
open ZBTest, ">$ARGV[0]\\zb_test.info" or die "$ARGV[0]\\zb_test.info";
open OtherTest, ">$ARGV[0]\\other_test.info" or die "$ARGV[0]\\other_test.info";

$fileNum = 0;
@sampleNum[4];

while ($file = readdir $dir) {
	$file_path = "$ARGV[0]\\$file";
	$match  = "\.feature";
	if($file_path =~ /$match/) {
		
		$fileNum++;
		open fin, "<$file_path" or die "can not open file $file_path!\n";		
		while (<fin>) {
			chomp;
			@info = split;

			#infomation
			$label = -1;
			$highSucNum = 0;
			$interval = 0;
			$rangeRSSI = 0;

			#features
			$smallerSucNum = -1;		# 1
			$biggerSucNum = -1;		# 2
			$shorterInterval = -1;		# 3
			$longerInterval = -1;		# 4
			$largerRangeRSSI = -1;		# 5
			$muchLargeRSSI = -1;		# 6
			
			#filtering the samples
			#ZigBee: (1) range <=5; (2) 300 < interval < 400; (3) 13 < length < 150; 
			#BT: (1) 9 <= length <= 23; if(length >13) rangeRSSI > 20
			#MO: (1) range >10;  if(length >13) rangeRSSI > 20
			#802.11n/g: (1) 6 <= length <= 20; (2) if(length<13) range has no restriction; if (length > 13) range >10;

			#get the information
			$label = $info[1];
			$rangeRSSI = $info[7];
			$highSucNum = $info[4];
			$interval = $info[10];
			$sucNum =  $info[9];

			#get features.
			if($highSucNum <=13){
				$smallerSucNum = 1;
			}
			else {
				$smallerSucNum = 0;
			}
			if($highSucNum >=150){
				$biggerSucNum = 1;
			}
			else {
				$biggerSucNum = 0;
			}
			if($interval<=300){
				$shorterInterval = 1;
			}
			else {
				$shorterInterval = 0;
			}
			if($interval>=400){
				$longerInterval = 1;
			}
			else {
				$longerInterval = 0;
			}	
			if($rangeRSSI>=5){
				$largerRangeRSSI = 1;
			}
			else {
				$largerRangeRSSI = 0;
			}
			if($rangeRSSI >= 20) {
				$muchLargeRSSI = 1;
			}
			else {
				$muchLargeRSSI = 0;
			}

			if($label!=0 && ($smallerSucNum==0 && $biggerSucNum==0 && $shorterInterval==0 && $longerInterval==0  && $largerRangeRSSI ==1 && $muchLargeRSSI ==0)){
						#print "$label 1:$smallerSucNum 2:$biggerSucNum 3:$shorterInterval 4:$longerInterval 5:$largerRangeRSSI 6:$muchLargeRSSI\n";
			}
			#$temp = $highSucNum -$sucNum;
			#if( $rangeRSSI<5 && $highSucNum < 20) {print "**********************$label \t $sucNum \t $highSucNum \t $temp\n";}
			#ZigBee
			if($label==0) {
				#if( $interval <400 && $interval >300 && $highSucNum <150 && $highSucNum >13) {
				if( $highSucNum <150 && $highSucNum >13) {
					$smallerSucNum = 0;
					$biggerSucNum = 0;
					$shorterInterval = 0;
					$longerInterval = 0;
					if($rangeRSSI <= 5) {
						$largerRangeRSSI = 0;
					}
					else {
						$largerRangeRSSI = 1;
					}
					print Output "1 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
					print Output1 "0 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
					print Train "1 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
					print ZBTest "1 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
					
					$sampleNum[$label]++;
				}
			}
			#BT
			if($label==1) {
				if($highSucNum<9 || $highSucNum >23) {
					$smallerSucNum = -1;
					$biggerSucNum = -1;
					$shorterInterval = -1;
					$longerInterval = -1;
					$largerRangeRSSI = -1;	
					$muchLargeRSSI = -1;
					next;
				}
				else {
					
					print Output "0 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
					print Output1 "1 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
					if(!($smallerSucNum==0 && $biggerSucNum==0 && $shorterInterval==0 && $longerInterval==0  && $largerRangeRSSI ==1 && muchLargeRSSI ==0)){
						#print "$label 1:$smallerSucNum 2:$biggerSucNum 3:$shorterInterval 4:$longerInterval 5:$largerRangeRSSI 6:$muchLarge\n";
						print Train "0 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
						$sampleNum[$label]++;
					}
					print OtherTest "0 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
					
				}
			}
			#MO
			if($label==2) {
				if($rangeRSSI<=10) {
					$smallerSucNum = -1;
					$biggerSucNum = -1;
					$shorterInterval = -1;
					$longerInterval = -1;
					$largerRangeRSSI = -1;	
					$muchLargeRSSI = -1;
					next;
				}
				else {
					print Output "0 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
					print Output1 "2 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
					if(!($smallerSucNum==0 && $biggerSucNum==0 && $shorterInterval==0 && $longerInterval==0  && $largerRangeRSSI ==1 && muchLargeRSSI ==0)){
						#print "$label 1:$smallerSucNum 2:$biggerSucNum 3:$shorterInterval 4:$longerInterval 5:$largerRangeRSSI 6:$muchLarge\n";
						print Train "0 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
						$sampleNum[$label]++;
					}
					print OtherTest "0 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";

					$sampleNum[$label]++;
				}
			}
			#WiFi 802.11n
			if($label==3) {
				if($highSucNum> 20 || $highSucNum <6){
					$smallerSucNum = -1;
					$biggerSucNum = -1;
					$shorterInterval = -1;
					$longerInterval = -1;
					$largerRangeRSSI = -1;	
					$muchLargeRSSI = -1;
					next;
				}
				else {
					if($highSucNum >13 && $rangeRSSI <10) {
						$smallerSucNum = -1;
						$biggerSucNum = -1;
						$shorterInterval = -1;
						$longerInterval = -1;
						$largerRangeRSSI = -1;
						$muchLargeRSSI = -1;
						next;						
					}
					else {
						print Output "0 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
						print Output1 "3 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
						if(!($smallerSucNum==0 && $biggerSucNum==0 && $shorterInterval==0 && $longerInterval==0  && $largerRangeRSSI ==1 && muchLargeRSSI ==0)){
							#print "$label 1:$smallerSucNum 2:$biggerSucNum 3:$shorterInterval 4:$longerInterval 5:$largerRangeRSSI 6:$muchLarge\n";
							print Train "0 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
							$sampleNum[$label]++;
						}
						print OtherTest "0 $smallerSucNum $biggerSucNum $shorterInterval $longerInterval $largerRangeRSSI $muchLargeRSSI\n";
						$sampleNum[$label]++;
					}
				}
			}
		}
		close fin;
	}
}
close Output;
close Output1;
close Train;
close ZBTest;
close OtherTest;

print "kinds: $fileNum\n";
$temp = 0;
for($i=0;$i<$fileNum;$i++) {
	print ("$i \t $sampleNum[$i]\n");
	$temp += $sampleNum[$i];
}
print ("total \t $temp\n");
